package com.panshi.controller.qa;

import com.panshi.api.IMessagApi;
import com.panshi.api.IRabbitMQApi;
import com.panshi.api.IReplyAuditApi;
import com.panshi.utils.GetSessionUser;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Messag;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.ReplyAudit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class ReplyAuditController {
    @Autowired
    IReplyAuditApi replyAuditApi;

   @Autowired
   IRabbitMQApi rabbitMQApi;

    @Autowired
    IMessagApi messagApi;



    /*IReplyAudit(回答审核)*/

    /**
     * 添加回答
     * @param replyAudit
     * @return
     */
    @PostMapping("/replyAudit/add")
    @ResponseBody
    DataResultQA addReplyAudit(@RequestBody ReplyAudit replyAudit){
        //获取登录后的用户id
        int loginUserId = GetSessionUser.getSessionUserId();
        replyAudit.setRUserId(loginUserId);
        DataResultQA dataResultQA = replyAuditApi.addReplyAudit(replyAudit);
        return dataResultQA;
    }

    /**
     * 分页查询
     * @param pages
     * @return
     */
    @PostMapping("/replyAudit/inquirePages")
    @ResponseBody
    DataResultQA<Pages<ReplyAudit>> inquireReplyAuditPages(@RequestBody Pages pages){
        DataResultQA<Pages<ReplyAudit>> dataResultQA = replyAuditApi.inquireReplyAuditPages(pages);
        return dataResultQA;
    }

    /**
     * 查询总记录数
     * @param pages
     * @return
     */
    @GetMapping("/replyAudit/inquireSum")
    @ResponseBody
    DataResultQA<Integer> inquireReplyAuditSum(@RequestParam Pages pages){
        DataResultQA<Integer> dataResultQA = replyAuditApi.inquireReplyAuditSum(pages);
        return dataResultQA;
    }

    /**
     * 通过id查询回答
     * @param id
     * @return
     */
    @PostMapping("/replyAudit/inquireById/{id}")
    @ResponseBody
    DataResultQA<ReplyAudit> inquireByIdReplyAudit(@PathVariable int id){
        DataResultQA<ReplyAudit> dataResultQA = replyAuditApi.inquireByIdReplyAudit(id);
        return dataResultQA;
    }

    /**
     * 修改内容（用户）
     * @param replyAudit
     * @return
     */
    @PostMapping("/replyAudit/update")
    @ResponseBody
    DataResultQA updateReplyAudit(@RequestBody ReplyAudit replyAudit){
        DataResultQA dataResultQA = replyAuditApi.updateReplyAudit(replyAudit);
        return dataResultQA;
    }

    /**
     * 删除(标记删除)
     * @param id
     * @return
     */
    @GetMapping("/replyAudit/delete/{id}")
    @ResponseBody
    DataResultQA deleteReplyAudit(@PathVariable int id){
        DataResultQA dataResultQA = replyAuditApi.deleteReplyAudit(id);
        return dataResultQA;
    }

    /**
     * 修改审核状态<审核>（管理员）
     * @param replyAudit
     * @return
     */
    @GetMapping("/replyAudit/updateState")
    @ResponseBody
    DataResultQA updateStateReplyAudit(@RequestParam ReplyAudit replyAudit){

        DataResultQA dataResultQA = replyAuditApi.updateStateReplyAudit(replyAudit);

        if(dataResultQA.isBoo()){
            //获取回答信息
            ReplyAudit ra = replyAuditApi.inquireByIdReplyAudit(replyAudit.getRId()).getData();

            //创建站内消息
            Messag messag = new Messag();

            //所属问题id
            int questionId = ra.getRQuestionId();

            //回答内容
            String content = ra.getRContent();
            messag.setContent(content);
            //审核状态
            int checkState = replyAudit.getRState();
            messag.setCheckState(checkState);
            //站内消息类型（0:公共 2:问题 3:回答 1:文章）
            int checkType = Messag.REPLY_TYPE;//(3：代表回答)
            messag.setCheckType(checkType);
            //描述
            String checkDescribe = replyAudit.getCheckDescribe();
            messag.setCheckDescribe(checkDescribe);
            //发送用户id
            int userId = ra.getRUserId();
            messag.setUserId(userId);

            //添加站内消息
            messagApi.addMessag(messag);

        }

        //rabbitmq发送邮件
       /* if (dataResultQA.isBoo()){
            rabbitMQApi.sendReplyAudit(replyAudit);
        }*/

        return dataResultQA;
    }
}
